import { defaultStatusMap } from '@/config/defaultStatus/defaultStatusMap'
import { defineStore } from 'pinia'
import {
  addOption,
  rmOption,
  setCurrentStatus,
  setPicLinkByIndex,
  setTextStatus,
  updateInitStatusBeforeAdd,
} from './actions'
import { ComsTypeEnum, type Material, type Status } from '@/types'

// 哪些业务组件要初始化
const initKey: Material[] = [ComsTypeEnum.personInfoGender, ComsTypeEnum.personInfoEdu]
const initState = {} as Record<Material, Status>
initKey.forEach((key) => {
  const defaultComStatus = defaultStatusMap[key]()
  updateInitStatusBeforeAdd(defaultComStatus, key)
  initState[key] = defaultComStatus
})
// 组件市场所有组件状态的仓库
export const useMaterialStore = defineStore('materialStore', {
  state: () => {
    return {
      currentMaterialCom: ComsTypeEnum.singleSelect, //当前选中的组件
      // 记录所有业务组件
      coms: {
        [ComsTypeEnum.singleSelect]: defaultStatusMap[ComsTypeEnum.singleSelect](), //执行函数
        [ComsTypeEnum.muSelect]: defaultStatusMap[ComsTypeEnum.muSelect](), //执行函数'
        [ComsTypeEnum.optionSelect]: defaultStatusMap[ComsTypeEnum.optionSelect](), //执行函数'
        [ComsTypeEnum.singlePicSelect]: defaultStatusMap[ComsTypeEnum.singlePicSelect](), //执行函数
        [ComsTypeEnum.muPicSelect]: defaultStatusMap[ComsTypeEnum.muPicSelect](), //执行函数
        // 业务组件
        [ComsTypeEnum.textNode]: defaultStatusMap[ComsTypeEnum.textNode](), //执行函数
        [ComsTypeEnum.personInfoGender]: initState[ComsTypeEnum.personInfoGender],
        [ComsTypeEnum.personInfoEdu]: initState[ComsTypeEnum.personInfoEdu],
      },
    }
  },
  actions: {
    setCurrentMaterialCom(comName: Material) {
      this.currentMaterialCom = comName
    },
    setTextStatus,
    addOption,
    rmOption,
    setCurrentStatus,
    setPicLinkByIndex,
  },
})
